package com.example.a10.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.a10.mapper.CosManager;
import com.example.a10.model.dto.upload.UploadRequest;
import com.example.a10.newentity.featureFile;
import com.example.a10.newentity.trainFile;
import com.example.a10.service.FileService;
import com.example.a10.service.trainFileService;
import com.example.a10.util.DocumentUtil;
import com.example.a10.util.FileUploadBizEnum;
import com.example.a10.util.UUIDUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Train")
public class TrainController {

    @Autowired
    trainFileService trainFileService;
    @Autowired
    FileService fileService;
    @Autowired
    CosManager cosmanager;

    @ApiOperation("上传训练文件")
    @PostMapping("/Upload")
    @Transactional(rollbackFor = Exception.class) // 添加事务注解
    public String TestUpload(@ModelAttribute UploadRequest uploadRequest) throws Exception {

        MultipartFile file = uploadRequest.getFile();
        //1.将数据存进云服务器
        // 选择上传文件的业务类型
        FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.Train;
        // 调用上传文件方法
        String Url = fileService.uploadFile(file, fileUploadBizEnum);

        if(Url!=null){
            //2.存进数据库
            trainFile trainFile = new trainFile();
            //生成id
            String trainId = UUIDUtil.getUUID();
            // 获取文件名
            String name = file.getOriginalFilename();
            //文件大小
            String filesize = DocumentUtil.getFileSize(file.getSize());
            String userid = uploadRequest.getUserid();
            trainFile.setId(trainId);
            trainFile.setFileName(name);
            trainFile.setFileSize(filesize);
            trainFile.setCreateTime(LocalDateTime.now());
            trainFile.setUrl(Url);
            trainFile.setUserId(userid);
            trainFileService.save(trainFile);
            return "success";
        }else {
            throw new Exception();
        }
    }

    @ApiOperation("返回训练文件名称，删除模型")
    @DeleteMapping("/delete")
    public String deleteTrain(@RequestBody trainFile trainFile) throws IOException {
        if(trainFile!=null){
            String trainFileId = trainFile.getId();
            String url = trainFile.getUrl();
            trainFileService.removeById(trainFileId);
            cosmanager.deleteFile(url);
            return "success";
        }else {
            return "failed";
        }
    }

    @ApiOperation("返回所有训练文件的所有名称")
    @GetMapping("/Trainlist")
    @ResponseBody
    public List<trainFile> selectTrainlist(String userid){
        LambdaQueryWrapper<trainFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(trainFile::getUserId,userid);
        List<trainFile> trainFileList = trainFileService.list(queryWrapper);
        return trainFileList;
    }
}
